package cn.edu.gues.infosystam.controller;


import cn.edu.gues.infosystam.entity.Cclass;
import cn.edu.gues.infosystam.entity.Student;
import cn.edu.gues.infosystam.service.CclassService;
import cn.edu.gues.infosystam.service.StudentService;
import cn.edu.gues.infosystam.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 苏小小
 * @since 2020-06-11
 */
@Controller
@RequestMapping("/Student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CclassService cclassService;

    @ResponseBody
    @PostMapping("/Del")
    public R delStudentBySno(String sno){
        System.out.println("学号："+sno);

        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
        queryWrapper.eq("sno", sno);
        boolean remove = studentService.remove(queryWrapper);
        if (remove)
            return R.success();
        else
            return R.error().message("删除学生失败！！！");
    }

    @ResponseBody
    @PostMapping("/Save")
    public R saveStudent(Student student){

        QueryWrapper<Cclass> queryWrapper = new QueryWrapper<Cclass>();

        queryWrapper.select("cid").eq("college",student.getCollege()).eq("cname",student.getCname());
        Cclass cclass = cclassService.getOne(queryWrapper);
        if (cclass != null){
            student.setCid(cclass.getCid());
            student.setSpassword("123456");
            boolean saveOrUpdate = studentService.saveOrUpdate(student);
            if (saveOrUpdate)
                return R.success().message("您成功的保存了一名学生的信息");
            return R.error().message("你输入的学生信息不合法，请检查后重新输入");
        }

        return R.error().message("您输入的信息不合法，无法保存");
    }
}

